<?php

	require("libraries/student.php");

  	class Gallery extends Student{

		public function __construct(){

			parent::__construct();

			$this->load->helper("url");

			$this->load->helper("form");

			$this->load->library("form_validation");

			$this->load->model("model_gallery");

		}

		public function index(){

			$config['base_url'] = base_url().'admin/gallery/index/';

			$config['total_rows'] = $this->model_gallery->count_all();

			$config['per_page'] = 10;

			$config['uri_segment'] = 4;

			$config['next_link'] = "Next";

			$config['prev_link'] = "Prev";

			$this->load->library("pagination");

			$this->pagination->initialize($config); 

			$start = $this->uri->segment(4);

			$data['data'] = "";

			$data['title']   = "Quản lý gallerys";

			$data['template'] = "gallerys/list_gallery";

			$data['act'] = 0;

			$data['listall']= $this->model_gallery->listall($config['per_page'],$start);

			$this->load->view("layout",$data);

		}

		public function add(){

			$data['data'] = "";

			$data['title']   = "Thêm mới hình ảnh";

			$data['template'] = "gallerys/add_gallery";

			$data['listalbum'] = $this->model_gallery->album();

			$data['act'] = 0;

			if($this->input->post("ok") != NULL){

				$title = $this->input->post("gallery_title");

				$db = array(

					"gallery_title" => $title,

					"album_id" => $this->input->post("album")

				);

				if($_FILES['image']['name'] != NULL){

					$config['upload_path'] = './uploads/albums/gallerys';

					$config['allowed_types'] = '*';

					$config['max_size']	= '3000';

					$config['max_width']  = '3000';

					$config['max_height']  = '3000';

					$this->load->library('upload',$config);

					if(!$this->upload->do_upload("image")){

						$data = array('error' => $this->upload->display_errors());

						$data['data'] = "";

						$data['title']   = "Thêm mới hình ảnh";

						$data['template'] = "gallerys/add_gallery";

						$data['listalbum'] = $this->model_gallery->album();

						$data['act'] = 0;

						$this->load->view("layout",$data);

						return FALSE;

					}else{

						$data = $this->upload->data();

						$db['gallery_name'] = $data['file_name'];

						$db['gallery_image'] = $data['file_name'];

						$this->createThumbnail($db['gallery_image'],"albums/gallerys",130,130);

						//$this->createThumb($db['gallery_image'],"albums/gallerys",440,240);

					}

					$this->model_gallery->add($db);

					redirect(base_url()."admin/gallery");

				}else{

					$data['error'] = "Bạn chưa chọn ảnh!";

					$this->load->view("layout",$data);

				}

			}else{

				$this->load->view("layout",$data);

			}

		}

		public function update(){

			$id = $this->uri->segment(4);

			$data['data'] = "";

			$data['title']   = "Sửa thông tin hình ảnh";

			$data['template'] = "gallerys/edit_gallery";

			$data['listalbum'] = $this->model_gallery->album();

			$data['getdata'] 	= $this->model_gallery->getdata($id);

			$data['stt'] = $data['getdata']['album_id'];

			$data['act'] = 0;

			if($this->input->post("ok") != NULL){

				$title = $this->input->post("gallery_title");

				$db = array(

					"gallery_title" => $title,

					"album_id" => $this->input->post("album")

				);

				if($_FILES['image']['name'] != NULL){

					$config['upload_path'] = './uploads/albums/gallerys';

					$config['allowed_types'] = '*';

					$config['max_size']	= '3000';

					$config['max_width']  = '3000';

					$config['max_height']  = '3000';

					$this->load->library('upload',$config);

					if(!$this->upload->do_upload("image")){

						$data = array('error' => $this->upload->display_errors());

						$data['data'] = "";

						$data['title']   = "Sửa thông tin hình ảnh";

						$data['template'] = "gallerys/edit_gallery";

						$data['listalbum'] = $this->model_gallery->album();

						$data['getdata'] 	= $this->model_gallery->getdata($id);

						$data['stt'] = $data['getdata']['album_id'];

						$data['act'] = 0;

						$this->load->view("layout",$data);

						return FALSE;

					}else{

						$data = $this->upload->data();

						$db['gallery_name'] = $data['file_name'];

						$db['gallery_image'] = $data['file_name'];

						$this->createThumbnail($db['gallery_image'],"albums/gallerys",130,130);

						//$this->createThumb($db['gallery_image'],"albums/gallerys",440,240);

					}

				}

				$this->model_gallery->update($db,$id);

				redirect(base_url()."admin/gallery");

			}else{

				$this->load->view("layout",$data);

			}

		}

		public function del(){

			$id = $this->uri->segment(4);

			$data = $this->model_gallery->getdata($id);

			@unlink("uploads/albums/gallerys/".$data['gallery_image']);

			@unlink("uploads/albums/gallerys/thumb/".$data['gallery_image']);

			@unlink("uploads/albums/gallerys/thumbs/".$data['gallery_image']);

			$this->model_gallery->del($id);

			redirect(base_url()."admin/gallery");

		}

		public function delall(){

			$val = $this->input->post("check");

			if($val != NULL){

				foreach($val as $items){

					$data = $this->model_gallery->getdata($items);

					@unlink("uploads/albums/gallerys/".$data['gallery_image']);

					@unlink("uploads/albums/gallerys/thumb/".$data['gallery_image']);

					@unlink("uploads/albums/gallerys/thumbs/".$data['gallery_image']);

					$this->model_gallery->del($items);

				}

				redirect(base_url()."admin/gallery");

			}

		}

		public function createThumbnail($fileName,$path,$width,$height){

			$this->load->library('image_lib');

			//$this->load->helper('thumbnail_helper');

			$config['image_library'] = 'gd2';

			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;

			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;

			$config['create_thumb'] = TRUE;

			$config['maintain_ratio'] = TRUE;

			$config['thumb_marker'] = FALSE;

			$config['width'] = $width;

			$config['height'] = $height;

			$this->image_lib->initialize($config); 

			$this->image_lib->resize();

			$this->image_lib->clear();

		}

		public function createThumb($fileName,$path,$width,$height){

			$this->load->library('image_lib');

			//$this->load->helper('thumbnail_helper');

			$config['image_library'] = 'gd2';

			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;

			$config['new_image']    = 'uploads/'.$path.'/thumbs/'.$fileName;

			$config['create_thumb'] = TRUE;

			$config['maintain_ratio'] = TRUE;

			$config['thumb_marker'] = FALSE;

			$config['maintain_ratio'] = FALSE;

			$config['width'] = $width;

			$config['height'] = $height;

			$this->image_lib->initialize($config); 

			$this->image_lib->resize();

			$this->image_lib->clear();

		}

	}